<?php

namespace app\api\controller;
use app\admin\model\AskingBack;
use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Asking extends Api
{
    protected $noNeedLogin = ['askingList','detail','back'];
    protected $noNeedRight = ['*'];
    protected $noNeedSite = ['myAskingList','detail','back'];

    /**
     * Notes: 卜咚问问-列表
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/26 0026
     * Time: 15:58
     */
    public function askingList(){
        $site_id = input('site_id',0);
        $keywords = input('keywords');
        $answered = input('answered');
        $user_id = $this->user_id;;
        $limit = input('limit');
        $hot = input('hot');
        $order = "createtime desc";
        $where=[];
        if(!empty($keywords)){
            $where['title']=['like','%'.trim($keywords).'%'];
        }
        if($answered==1){
            $where['comment'] = 0;
        }
        if($hot){
            $order = "comment desc createtime desc";
        }
        $where['site_id'] = $site_id;
        if(!empty($user_id)){
            $where['user_id'] = $user_id;
        }
        $field="";
        $askingModel = new \app\admin\model\Asking();
        $askingData = $askingModel->_list($field,$where,$limit,$order);
        $this->success('成功',$askingData);
    }

    /**
     * Notes: 卜咚问问-详情
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/26 0026
     * Time: 17:12
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail(){
        $asking_id = input('asking_id');
        if(empty($asking_id)){
            $this->error('缺少参数');
        }
        $asking = new \app\admin\model\Asking();
        $detail = $asking->where('id',$asking_id)->find();
        $detail['user'] = (new \app\common\model\User())->field('id,username,nickname,avatar')->find($detail['user_id']);
        if(!empty($detail)){
            if(!$detail['status'])$this->error('该问题已删除');
            $asking->where('id',$asking_id)->setInc('reading');
            $this->success('成功',$detail);
        }
        $this->error('无此问题');
    }

    /**
     * Notes: 卜咚问问-提问
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/26 0026
     * Time: 15:33
     */
    public function addAsking(){
        $param = $this->request->param();
        $validate = new \app\admin\validate\Asking();
        if(!$validate->check($param)){
            $this->error($validate->getError());
        }
        if(!isset($_POST['postcontent']) || empty($_POST['postcontent']))$this->error('内容不可为空');
        $param['postcontent'] = $_POST['postcontent'];
        $askingModel = new \app\admin\model\Asking();
        if($askingModel->allowField(true)->save($param)){
            $this->success('提问成功');
        }else{
            $this->error('提问失败');
        }
    }

    /**
     * Notes: 问题回复
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/26 0026
     * Time: 17:51
     */
    public function answered(){
        $asking_id = input('asking_id');
        $user_id = $this->user_id;;
        $content = input('content');
        if(empty($asking_id))$this->error('缺少参数');
        if(empty($content))$this->error('请填写回复内容');
        if(mb_strlen($content)>200)$this->error('回复内容不可超过200字');
        $askingBackModel = new AskingBack();
        $data=[
            'user_id'=>$user_id,
            'asking_id'=>$asking_id,
            'content'=>$content
        ];
        if($askingBackModel->save($data)){
            $asking = new \app\common\model\Asking();
            $asking->where('id',$asking_id)->setInc('comment');
            $this->success('回复成功');
        }else{
            $this->error('回复失败');
        }
    }

    /**
     * Notes: 卜咚问问-回答列表
     * User: hawk
     * Email: 2271030162@qq.com
     * Date: 2019/6/26 0026
     * Time: 17:39
     * @throws \think\exception\DbException
     */
    public function back(){
        $asking_id = input('asking_id');
        $limit = input('limit');
        if(empty($asking_id)){
            $this->error('缺少参数');
        }
        $askingBackModel = new AskingBack();
        $where['asking_id'] = $asking_id;
        $where['status'] = '1';
        $order = "is_true desc createtime desc";
        $askingBackData = $askingBackModel
            ->with(['user'])
            ->where($where)
            ->order($order)
            ->paginate($limit);
        $this->success('成功',$askingBackData);
    }
}